/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectPanel.java
 *
 * Created on 14 déc. 2011, 11:33:07
 */

package chatsystem.design;

import chatsystem.Controller;
import javax.swing.JOptionPane;

/**
 *
 * @author Maël
 */
public class ConnectPanel extends javax.swing.JPanel {

    private Controller controller;
    private ChatGUI gui;
    
    /**
     * Crée un nouveau ConnectPanel
     * @param c le controller
     * @param gui le Gui
     */
    public ConnectPanel(Controller c, ChatGUI gui) {
        this.controller = c;
        this.gui = gui;
        initComponents();
    }
    
    //Méthode initComponents() générée par l'éditeur graphique
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        connectButton = new javax.swing.JButton();

        jLabel1.setText("Welcome to ChatSystem");

        jLabel2.setText("Username :");

        connectButton.setText("Start chatting!");
        connectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connectButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel2)
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(connectButton))
                            .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    /**
     * Définit ce qu'on fait lorsque l'on clique sur le bouton de connexion
     */
    private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectButtonActionPerformed
        /* Si l'utilisateur n'a pas entré de username (texte vide)
         * On ouvre une boite de dialogue d'erreur demandant d'entrer un nom
         */
        if (this.usernameField.getText().equals("")) {
            JOptionPane.showMessageDialog(
                this, // fenêtre parente
                "Veuillez entrer un nom svp", // corps du dialogue
                "Erreur : username vide",// Titre du dialogue
                JOptionPane.ERROR_MESSAGE // type du dialogue
            );
        }
        /* Sinon => username non vide
         * On se connecte
         */
        else {
            this.controller.connect(this.usernameField.getText());
            gui.setConversationWindow();
        }
    }//GEN-LAST:event_connectButtonActionPerformed

    //Variables déclarées automatiquement par l'éditeur graphique
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField usernameField;
    // End of variables declaration//GEN-END:variables

}
